import { ProxyBase } from "./ProxyBase";
import { Constructor, Node } from "cc";
import { proxyMgr } from "./ProxyMgr";
import { SpriteFrame } from "cc";
import { SpriteAtlas } from "cc";
import { sp } from "cc";
import { ProxyMeta } from "./BaseTypes";
import { resMgr } from "../res/ResMgr";
import { assetManager } from "cc";
import { Prefab } from "cc";

/**
 * 需要绑定代理的类==>继承此类
 */
export class ProxyWnd<T extends ProxyBase> {
    protected _view: T;
    get view() {
        return this._view
    }

    private _meta: Readonly<ProxyMeta> = null;
    get meta() {
        return this._meta
    }

    get uuid() {
        return this._view.uuid;
    }


    constructor(view: T, meta: ProxyMeta) {
        this._view = view;
        this._meta = meta;
    }

    /**
     * 创建一个子窗口实例
     */
    protected createSubWnd(cls: Constructor<ProxyBase>, parentNode: Node) {
        return proxyMgr.createSubWnd(cls.name, this, parentNode)
    }

    /**
     * 加载同属bundle内的资源
     * @param type 
     * @returns 
     */
    protected async loadRes(path: string, type: Constructor<SpriteFrame | SpriteAtlas | sp.SkeletonData | Prefab>) {
        return proxyMgr.loadWndBundleRes(this, this._meta.bundle, path, type)
    }

    /**
     * 加载同属bundle内的文件夹内的某一类资源
     * @param dir 
     */
    protected async loadDirRes(dir: string, type: Constructor<SpriteFrame | SpriteAtlas | sp.SkeletonData | Prefab>) {
        const paths = assetManager.getBundle(this._meta.bundle).getDirWithPath(dir, type).map(a => a.path);
        return Promise.all(paths.map(path => this.loadRes(path, type)));
    }

    /**
     * 获取bundle内的资源（同属bundle和公共bundle内的）
     * @param path 
     * @param bundle
     */
    protected getRes(path: string, bundle?: string) {
        return resMgr.getRes(path, bundle?? this._meta.bundle);
    }
    /*************************可继承函数***********************/

    /**
      * 初始化信息
      */
    onInit() {

    }

    /**
     * 销毁
     */

    onDestroy() {
    }
    /*************************可继承函数***********************/
}